const {
    Mysql
} = require('../tools/utils');
module.exports = async (ctx) => {
    let {
        operate
    } = ctx.query;
    let {
        imgUrl,
        type,
        adUrl
    } = ctx.request.body;
    if (!operate) {
        let sql = `INSERT INTO swiper(imgUrl,type,adUrl) VALUES('${imgUrl}',${type},'${adUrl}')`
        try {
            let res = await Mysql(sql);
            if (res) {
                ctx.state.data = {
                    msg: 'مۇۋەپپەقىيەتلىك بولدى',
                    code: '001'
                }
            }
        } catch (error) {
            ctx.state.data = {
                msg: 'مەغلۇپ بولدى',
                code: '002'
            }
        }
    }
    if (operate == 'edit') {
        let {
            id
        } = ctx.request.body;
        let sql = `UPDATE swiper SET imgUrl='${imgUrl}',type=${type},adUrl='${adUrl}' WHERE id=${id}`;
        try {
            let res = await Mysql(sql);
            if (res) {
                ctx.state.data = {
                    msg: 'مۇۋەپپەقىيەتلىك بولدى',
                    code: '001'
                }
            }
        } catch (error) {
            ctx.state.data = {
                msg: 'مەغلۇپ بولدى',
                code: '002'
            }
        }
    }
    if (operate == 'delete') {
        let {
            id
        } = ctx.request.body;
        let sql = `DELETE FROM swiper WHERE id=${id}`;
        try {
            let res = await Mysql(sql);
            if (res) {
                ctx.state.data = {
                    msg: 'مۇۋەپپەقىيەتلىك بولدى',
                    code: '001'
                }
            }
        } catch (error) {
            ctx.state.data = {
                msg: 'مەغلۇپ بولدى',
                code: '002'
            }
        }
    }
}